<?php
namespace app\applet\model;
use think\Db;
use think\Model;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/5
 * Time: 16:25
 */
class HousingTemplate extends Model
{
    protected $table = 'housing_template';
    /**
     * 住房模板管理
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function templateList(){
        //后台朝向数据
        $date=Db::name($this->table)
            ->field('te_root, te_guest_room, te_toilet')
            ->order('te_root asc, te_guest_room asc, te_toilet asc')
            ->distinct('te_root, te_guest_room, te_toilet')
            ->select();
        return $date;
    }
    /**
     * 住房房源模板添加
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function templateAdd($data){
        //添加操作用户id
        /*$uid=get_user_id($data['token']);
        $arr['te_uid'] = $uid;*/
        //处理数据
        $arr['te_uid'] = 2;
        $arr['te_name'] = isset($data['te_name']) ? trim($data['te_name']) : "";
        $arr['te_area'] = isset($data['te_area']) ? trim($data['te_area']) : "";
        $arr['te_money'] = isset($data['te_money']) ? trim($data['te_money']) : "";
        $arr['te_root'] = isset($data['te_root']) ? trim($data['te_root']) : "";
        $arr['te_guest_room'] = isset($data['te_guest_room']) ? trim($data['te_guest_room']) : "";
        $arr['te_toilet'] = isset($data['te_toilet']) ? trim($data['te_toilet']) : "";
        $arr['te_orientation'] =  isset($data['te_orientation']) ? trim($data['te_orientation']) : "";
        $arr['te_floor_plans'] = isset($data['te_floor_plans']) ? implode(',',$data['te_floor_plans']) : "";
        $arr['te_indoor_drawings'] = isset($data['te_indoor_drawings']) ? implode(',',$data['te_indoor_drawings']) : "";
        $arr['te_matching'] = isset($data['te_matching']) ? implode(',',$data['te_matching']) : "";
        $arr['te_a_time'] = time();
        $data=Db::name('housing_template')->insert($arr);
        return $data;
    }


    /**
     * 住房资源模板查询
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function templategOne($id){
        //处理数据
        $data=Db::name('housing_template')
            ->where(['template_id'=>$id])
            ->where(['te_state'=>1])
            ->find();
        $data['te_floor_plans']=explode(',',$data['te_floor_plans']);
        $data['te_indoor_drawings']=explode(',',$data['te_indoor_drawings']);
        $data['te_matching']=explode(',',$data['te_matching']);
        return $data;
    }
    /**
     * 住房模板修改
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function templateUpdate($data){
        //处理数据
        $where['template_id']=$data['template_id'];
        //处理数据
        $arr['te_name'] = isset($data['te_name']) ? trim($data['te_name']) : "";
        $arr['te_area'] = isset($data['te_area']) ? trim($data['te_area']) : "";
        $arr['te_money'] = isset($data['te_money']) ? trim($data['te_money']) : "";
        $arr['te_root'] = isset($data['te_root']) ? trim($data['te_root']) : "";
        $arr['te_guest_room'] = isset($data['te_guest_room']) ? trim($data['te_guest_room']) : "";
        $arr['te_toilet'] = isset($data['te_toilet']) ? trim($data['te_toilet']) : "";
        $arr['te_orientation'] = isset($data['te_orientation']) ? trim($data['te_orientation']) : "";
        $arr['te_floor_plans'] = isset($data['te_floor_plans']) ? implode(',',$data['te_floor_plans']) : "";
        $arr['te_indoor_drawings'] = isset($data['te_indoor_drawings']) ? implode(',',$data['te_indoor_drawings']) : "";
        $arr['te_matching'] = isset($data['te_matching']) ? implode(',',$data['te_matching']) : "";
        $arr['te_u_time'] = time();
        $data=Db::name('housing_template')->where($where)->update($arr);
        return $data;
    }
    /**
     * 住房模板修改
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function index(){
        //处理数据
        $where['te_state']=1;
        $data=Db::name('housing_template')
            ->alias('a')
            ->join('admin_user','a.te_uid=admin_user.admin_id')
            ->field('template_id,te_root,te_area,te_guest_room,te_name,te_a_time,te_toilet,name')
            ->where($where)->select();
        //var_dump($data);exit;
        return $data;
    }
    /**
     * 住房模板删除
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function templateDel($data){
        //处理数据
        $where['template_id']=$data['template_id'];
        $data=Db::name('housing_template')
            ->where($where)
            ->update(['te_state'=>2]);
        return $data;
    }

    /**单元楼显示
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function unit(){
        //单元楼数据
        $data=Db::name('unit_building')
            ->field('tung_num,unit_num,layer_num,floor_type')
            ->where(['u_type'=>1])
            ->order('tung_num asc,unit_num asc')
            ->select();
        foreach($data as $k=>$v){
            $data[$k]['unit']=$v['tung_num'].'-'.$v['unit_num'];
        }
        return $data;
    }
    /**单元楼模板显示
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function unitTemplate($id){
        //单元楼数据
        $data=Db::name('housing_unit')
            ->field('h_u_id,tung_u_num,unit_u_num,layer_u_num')
            ->where(['t_u_id'=>$id])
            ->order('tung_u_num asc,unit_u_num asc,layer_u_num asc')
            ->select();
        if($data!=null){
            foreach($data as $k=>$v){
                $data[$k]['tung']=$v['tung_u_num'].'栋'.$v['unit_u_num'].'单元'.$v['layer_u_num'].'层';
            }
        }

        return $data;
    }
    /**单元楼和模板关联添加
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function unitTemplateAdd($data){
        $arr['tung_u_num'] = isset($data['tung_u_num']) ? trim($data['tung_u_num']) : "";
        $arr['unit_u_num'] = isset($data['unit_u_num']) ? trim($data['unit_u_num']) : "";
        $arr['layer_u_num'] = isset($data['layer_u_num']) ? trim($data['layer_u_num']) : "";
        $datas=Db::name('housing_unit')->where($arr)->find();
        //判断楼层是否已被关联
        if($datas!=null){
            return 2;
        }
        $arr['t_u_id'] = isset($data['t_u_id']) ? trim($data['t_u_id']) : "";
        $arr['tu_a_time'] = time();
        //单元楼数据
        $data=Db::name('housing_unit')->insert($arr);
        //$data=Db::name('housing_unit')->insertGetId($arr);
        return $data;
    }
    /**单元楼和模板关联删除
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function unitTemplateDel($id){
        //单元楼数据
        $cpunt=Db::name('housing_unit')
            ->field('t_u_id')
            ->where(['h_u_id'=>$id])
            ->find();
        $data=Db::name('housing_unit')
            ->where(['h_u_id'=>$id])
            ->delete();
        $data=$this->unitTemplate($cpunt['t_u_id']);
        /*$data=Db::name('housing_unit')
            ->where(['t_u_id'=>$cpunt['t_u_id']])
            ->select();*/
        return $data;
    }
    /**选择单元楼楼层返回模板
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function TemplateChoice($data){
        //单元楼数据
        $arr['tung_u_num'] = $data['tung_u_num'];
        $arr['unit_u_num'] =$data['unit_u_num'];
        $arr['layer_u_num'] = $data['layer_u_num'];
        $datas=Db::name('housing_unit')
            ->field('t_u_id')
            ->where($arr)->find();
        $date=Db::name('housing_template')
            ->where(['template_id'=>$datas['t_u_id']])->find();
        //判断楼层是否已被关联
        return $date;
    }
    /**选择单元楼楼层返回模板
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function TemplateSe($id){
        //单元楼数据
        $date=Db::name('housing_template')
            ->where(['template_id'=>$id])->find();
        //判断楼层是否已被关联
        return $date;
    }
}